export function formatTimeStamp(timestamp) {
    const now = Date.now();
    const diff = now - timestamp;

    const oneMinute = 60 * 1000;
    const oneHour = 60 * oneMinute;
    const oneDay = 24 * oneHour;
    const oneMonth = 30 * oneDay; // 近似一个月
    const threeMonths = 3 * oneMonth;

    if (diff < oneMinute) {
        return "1分钟内";
    } else if (diff < oneHour) {
        const minutes = Math.floor(diff / oneMinute);
        return `${minutes}分钟`;
    } else if (diff < oneDay) {
        const hours = Math.floor(diff / oneHour);
        return `${hours}小时`;
    } else if (diff < oneMonth) {
        const days = Math.floor(diff / oneDay);
        return `${days}天`;
    } else if (diff < threeMonths) {
        const months = Math.floor(diff / oneMonth);
        return `${months}个月`;
    } else {
        return null;
    }
}

//封装弹窗
export function showToast(title="",icon="none",mask=true){
	let posttion = icon=='none' ? 'bottom' :"center";
	uni.showToast({
		title,
		icon,
		mask,
		posttion
	})
}


//审核状态组
export const stateLists = [{
	"value": 0,
	"text": "审核中",
	"color":"#F3A73F",
	"bgColor":"#FDEDD9"
},
{
	"value": 1,
	"text": "审核通过",
	"color":"#18BC37",
	"bgColor":"#D1F2D7"
},
{
	"value": 2,
	"text": "审核不通过",
	"color":"#E43D33",
	"bgColor":"#FAD8D6"
}]

//传入value值返回，状态对象
export function stateFormat(value){
	return stateLists.find(item=>item.value == value)
}


// 方法比较两个日期字符串 '2023-02-01', '2024-01-01'
export function compareDates(date1, date2){
	const d1 = new Date(date1);
	const d2 = new Date(date2);
	
	// 返回比较结果
	return d1 >= d2 ? true : false;
};


// 获取当前的时间
export function getCurrent(type){
	const now = new Date();
	
	if (type === 'date') {
		const year = now.getFullYear();
		const month = String(now.getMonth() + 1).padStart(2, '0'); // 月份从0开始
		const day = String(now.getDate()).padStart(2, '0');
		return `${year}-${month}-${day}`;
	} else if (type === 'time') {
		const hours = String(now.getHours()).padStart(2, '0');
		const minutes = String(now.getMinutes()).padStart(2, '0');
		return `${hours}:${minutes}`;
		// const seconds = String(now.getSeconds()).padStart(2, '0');
		// return `${hours}:${minutes}:${seconds}`;
	}
	return now;
};

export function getTimestampByStr(dateStr, timeStr){
	const dateTimeStr = `${dateStr} ${timeStr}:00`
	const dateTime = new Date(dateTimeStr)
	return dateTime.getTime()
	// return Math.floor(dateTime.getTime() / 1000);
}

// 传入一个时间戳，输出一个文案，如果是今天时间，返回“今天”，如果是明天，返回“明天”，
// 如果是后天，返回“后天”，否则就返回月份和日期，如“3月12日”
export function getTimeText(timestamp){
	const date = new Date(timestamp);
	const today = new Date();
	const tomorrow = new Date(today);
	const dayAfterTomorrow = new Date(today);
	
	tomorrow.setDate(today.getDate() + 1);
	dayAfterTomorrow.setDate(today.getDate() + 2);
	
	// 比较日期
	if (date.toDateString() === today.toDateString()) {
		return "今天";
	} else if (date.toDateString() === tomorrow.toDateString()) {
		return "明天";
	} else if (date.toDateString() === dayAfterTomorrow.toDateString()) {
		return "后天";
	} else {
		const month = date.getMonth() + 1; // 月份从0开始
		const day = date.getDate();
		return `${month}月${day}日`;
	}
}

// 将地区转换成指定格式，例如输入“广东省广州市增城区”，得到“广州 | 增城区”
export function convertRegion(region) {
	const regex = /([\u4e00-\u9fa5]+省|自治区|直辖市|特别行政区)([\u4e00-\u9fa5]+市)([\u4e00-\u9fa5]+区|县)/;
	const match = region.match(regex);
	
	if (match) {
		return `${match[2]} | ${match[3]}`;
	} else {
		return region;
	}
}

// 例如字符串是“广东省广州市天河区”，传入province，就可以返回“广东省”
export function getRegionInfo(regionString, type) {
  // 正则表达式匹配省、市、区
  const regex = /([\u4e00-\u9fa5]+省|自治区|直辖市|特别行政区)([\u4e00-\u9fa5]+市)([\u4e00-\u9fa5]+区|县)/;
  const match = regionString.match(regex);
  
  if (match) {
    const matchedProvince = match[1]; // 匹配的省
    const matchedCity = match[2]; // 匹配的市
    const matchedArea = match[3]; // 匹配的区

    // 根据 type 返回对应的值
    switch (type) {
      case 'province':
        return matchedProvince;
      case 'city':
        return matchedCity;
      case 'area':
        return matchedArea;
      default:
        return '';
    }
  }
}

export function getHourMinByTimestamp(timestamp) {
  // 创建一个新的 Date 对象
  const date = new Date(timestamp);
  
  // 获取小时和分钟
  const hours = String(date.getHours()).padStart(2, '0'); // 补零到两位
  const minutes = String(date.getMinutes()).padStart(2, '0'); // 补零到两位

  // 返回格式化的时间
  return `${hours}:${minutes}`;
}